import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from '../views/Home.vue'
import User from '../views/User.vue'
import Question from '../views/Question.vue'
import Questionset from '../views/Questionset.vue'
import Contest from '../views/Contest.vue'
import Search from '../views/Search.vue'
import Team from '../views/Team.vue'
import Answersheet from '../views/answersheet.vue'
import Findmate from '../views/Findmate.vue'
import Adminpage from '../views/Adminpage.vue'

import PersonalHome from '../components/PersonalHome.vue'
import PersonalSetting from '../components/PersonalSetting.vue'
import Security from '../components/Security.vue'
import Myquestion from '../components/Myquestion.vue'
import Mymatch from '../components/Mymatch.vue'
import Managesection from '../components/Managesection.vue'
import Managethread from '../components/Managethread.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/Findmate',
    name: 'Findmate',
    component: Findmate
  },
  {
    path: '/search',
    name: 'Search',
    component: Search
  },
  {
    path: '/question',
    name: 'Question',
    component: Question
  },
  {
    path: '/questionset',
    name: 'Questionset',
    component: Questionset
  },
  {
    path: '/contest',
    name: 'Contest',
    component: Contest
  },
  {
    path: '/team',
    name: 'Team',
    component: Team
  },
  {
    path: '/adminpage',
    component: Adminpage,
    children: [
      {
        path: '',
        component: Managesection
      },
      {
        path: 'managesection',
        name: 'Managesection',
        component: Managesection
      },
      {
        path: 'managethread',
        name: 'Managethread',
        component: Managethread
      }
    ]
  },
  {
    path: '/answersheet',
    name: "answersheet",
    component: Answersheet
  },
  {
    path: '/user',
    component: User,
    children: [
      {
        path: '',
        name: 'PersonalHome',
        component: PersonalHome
      },
      {
        path: 'setting',
        name: 'PersonalSetting',
        component: PersonalSetting
      },
      {
        path: 'myquestion',
        name: 'Myquestion',
        component: Myquestion
      },
      {
        path: 'mymatch',
        name: "Mymatch",
        component: Mymatch
      },
      {
        path: 'security',
        name: 'Security',
        component: Security
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {

  let getFlag = sessionStorage.getItem('token');
  //console.log(getFlag+"zz")
  if (to.path === '/')
    next();
  else if (to.path !== '/') {
    if (getFlag === null || getFlag === "") {
      next('/')
      alert("请先登录")
    }
    else if (to.path == '/Adminpage') {
      if (sessionStorage.getItem('username') !== "caiyunqi" && sessionStorage.getItem('username') !== "soledad") {
        alert("无权限")
        next('/')
      }
      else next()
    }
    else {
      next();
    }
  }
});
export default router
